package engine.systems.update.tween
{
	import com.greensock.TweenMax;
	
	import engine.classes.enumerations.SystemPriority;
	import engine.systems.update.GameSystem;
	
	import org.as3commons.collections.framework.core.LinkedMapIterator;
	
	public class TweenSystem extends GameSystem
	{
		private var iterator:LinkedMapIterator;
		private var tween:TweenMax;
		
		public function TweenSystem()
		{
			super(TweenNode, updateNode);
			
			this.priority = SystemPriority.UPDATE;
		}
		
		public function updateNode(node:TweenNode, time:Number):void
		{
			if(node.tween.paused) return;
			
			iterator = node.tween.tweens.iterator() as LinkedMapIterator;
			while(iterator.hasNext())
			{
				tween = iterator.next();
				
				if(tween.totalProgress < 1)
				{
					if(tween.delay > 0) tween.delay -= time;
					else tween.totalTime += time;
				}
				else
				{
					iterator.remove();
					tween.kill();
					tween = null;
				}
			}
		}
	}
}